import {deepClone} from "@/utils/ruoyi";

export const converArr = (data) => {
  let tempArr = [], newArr = []
  for (let i = 0; i < data.length; i++) {
    const item = data[i]
    if (tempArr.indexOf(item.type) === -1) {
      newArr.push({
        name: item.tabName,
        type: item.type,
        list: [item]
      })
      tempArr.push(item.type);
    } else {
      for (let j = 0; j < newArr.length; j++) {
        if (newArr[j].type == item.type) {
          newArr[j].list.push(item)
        }
      }
    }
  }
  return newArr
}


// 对组件默认值处理
export const handleDefaultValue = (controllerJson) => {
  for (const key in controllerJson) {
    if (key == "options") {
      // collapse、data、pos、setting
      // setting 处理
      for (let i = 0; i < controllerJson.options.setting.length; i++) {
        const item = controllerJson.options.setting[i];
        if (Object.prototype.toString.call(item) == "[object Object]") {
          controllerJson.value.setting[item.name] = item.value;
        } else if (
          Object.prototype.toString.call(item) == "[object Array]"
        ) {
          for (let j = 0; j < item.length; j++) {
            const list = item[j].list;
            list.forEach(el => {
              controllerJson.value.setting[el.name] = el.value;
            });
          }
        }
      }
      // pos
      for (let i = 0; i < controllerJson.options.pos.length; i++) {
        const item = controllerJson.options.pos[i];
        if (item.value) {
          controllerJson.value.pos[item.name] = item.value;
        }
      }
      // data 处理
      if (controllerJson.options.data && controllerJson.options.data.length > 0) {
        for (let i = 0; i < controllerJson.options.data.length; i++) {
          const item = controllerJson.options.data[i];
          if (item.value) {
            controllerJson.value.data[item.name] = item.value;
          }
        }
      }
    }
  }
  return controllerJson;
}

// 设置默认
export const setDefaultValue = (options, val) => {
  for (let i = 0; i < options.length; i++) {
    if (Object.prototype.toString.call(options[i]) == "[object Object]") {
      for (const k in val) {
        if (options[i].name == k) {
          options[i].value = val[k];
        }
      }
    } else if (
      Object.prototype.toString.call(options[i]) == "[object Array]"
    ) {
      for (let j = 0; j < options[i].length; j++) {
        const list = options[i][j].list;
        for (let z = 0; z < list.length; z++) {
          for (const k in val) {
            if (list[z].name == k) {
              list[z].value = val[k];
            }
          }
        }
      }
    }
  }
}


export const handlerLayerController = (val, getToolByCode) => {
  const layerControllerArr = [];
  for (let i = 0; i < val.length; i++) {
    const obj = {};
    obj.icon = getToolByCode(val[i].type).icon;
    const options = val[i].options["setting"];
    options.forEach(el => {
      if (el.name == "layerName") {
        obj.label = el.value;
      }
    });
    layerControllerArr.push(obj);
  }
  return layerControllerArr;
}


export const handleInitProgramData = (data, getToolByCode) => {
  debugger;
  const pageInfoList = data ? data.pageInfoList : [];
  const pageInfoData = [];
  for (let i = 0; i < pageInfoList.length; i++) {
    const controllerInfoList = pageInfoList[i].controllerInfoList;
    const controllerInfoData = [];
    for (let x = 0; x < controllerInfoList.length; x++){
      let obj = {};
      obj.type = controllerInfoList[x].controllerType;
      obj.value = {
        setting: JSON.parse(controllerInfoList[x].setting),
        data: JSON.parse(controllerInfoList[x].data),
        pos: JSON.parse(controllerInfoList[x].pos)
      };
      if (!obj.type) continue;
      const tool = deepClone(getToolByCode(obj.type))
      const option = tool.options;
      const options = handleOptionsData(obj.value, option);
      obj.options = options;
      controllerInfoData.push(obj);
    }
    let pageObj = {}
    pageObj.pageName = pageInfoList[i].pageName;
    pageObj.width = pageInfoList[i].width;
    pageObj.height = pageInfoList[i].height;
    pageObj.backgroundColor = pageInfoList[i].backgroundColor;
    pageObj.backgroundImage = pageInfoList[i].backgroundImage;
    pageObj.controllerInfoList = controllerInfoData;
    pageInfoData.push(pageObj);
  }
  return pageInfoData;
}


export const handleBigScreen = (data, getToolByCode, callBcak) => {

  const optionScreen = getToolByCode("screen").options;
  const setting = optionScreen.setting;
  for (const key in data) {
    for (let i = 0; i < setting.length; i++) {
      if (key == setting[i].name) {
        setting[i].value = data[key];
      }
    }
  }
  if (callBcak)
    callBcak();
  return {
    backgroundColor: (data && data.backgroundColor) || "",
    backgroundImage: (data && data.backgroundImage) || "",
    height: (data && data.height) || "1080",
    pageName: (data && data.pageName) || "",
    width: (data && data.width) || "1920"
  };
}

export const handleOptionsData = (data, option) => {
  for (let key in data.setting) {
    for (let i = 0; i < option.setting.length; i++) {
      let item = option.setting[i];
      if (Object.prototype.toString.call(item) == "[object Object]") {
        if (key == option.setting[i].name) {
          option.setting[i].value = data.setting[key];
        }
      } else if (Object.prototype.toString.call(item) == "[object Array]") {
        for (let j = 0; j < item.length; j++) {
          const list = item[j].list;
          list.forEach(el => {
            if (key == el.name) {
              el.value = data.setting[key];
            }
          });
        }
      }
    }
  }
  // pos
  for (let posKey in data.pos) {
    for (let i = 0; i < option.pos.length; i++) {
      if (posKey == option.pos[i].name) {
        option.pos[i].value = data.pos[posKey];
      }
    }
  }
  // data
  for (let key in data.data) {
    for (let i = 0; i < option.data.length; i++) {
      if (key == option.data[i].name) {
        option.data[i].value = data.data[key];
      }
    }
  }
  return option;
}

/**
 * 根据标签和配置项获取值
 * @param label
 * @param options
 * @returns {number|*}
 */
export const getOptionsValueByLabel = (label, options) => {
  for (let i = 0;  i < options.pos.length; i++) {
    if (label == options.pos[i].label) {
      return options.pos[i].value;
    }
  }
  return 0;
}
